package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service

public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;
    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        Long total = page.getTotal();

        List<SetmealVO> setmealVOS = page.getResult();


        return new PageResult(total,setmealVOS);
    }

    /**
     * 插入套餐数据
     * @param setmealDTO
     */

    @Transactional
    @Override
    public void insert(SetmealDTO setmealDTO) {
            Setmeal setmeal = new Setmeal();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        BeanUtils.copyProperties(setmealDTO,setmeal);

        if (setmealDishes == null) {
            throw new  SetmealEnableFailedException("菜品为空");
        }

        List<Long> dishIds = new ArrayList<>();

        for (SetmealDish setmealDish : setmealDishes) {
            dishIds.add(setmealDish.getDishId());

        }
        if (dishMapper.checkStop(dishIds) > 0) {
            throw new SetmealEnableFailedException("套餐含未启售的菜品");
        }


        setmealMapper.insertSetmeal(setmeal);

        for (SetmealDish setmealDish : setmealDishes) {

            setmealDish.setSetmealId(setmeal.getId());

        }

        setmealDishMapper.insertSetmealDish(setmealDishes);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {

        SetmealVO setmealVO = setmealMapper.selectById(id);

        setmealVO.setSetmealDishes(setmealDishMapper.selectBySetmealId(id));

        return setmealVO;
    }

    /**
     * 更新套餐和套餐菜品
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateWithSetmealDish(SetmealDTO setmealDTO) {

        if (setmealDTO==null){
            throw new SetmealEnableFailedException("套餐数据为空");
        }

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }

        List<Long> dishIds = new ArrayList<>();

        dishIds.add(setmeal.getId());

        setmealDishMapper.deleteBySetmealIds(dishIds);

        setmealDishMapper.insertSetmealDish(setmealDishes);



    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {

        for (Long id : ids) {

            SetmealVO setmealVO = setmealMapper.selectById(id);

            if (setmealVO.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

        }

        setmealMapper.deleteByIds(ids);

        setmealDishMapper.deleteBySetmealIds(ids);

    }

    @Override
    public void stopOrStart(Integer status, Long id) {

        if (status!= null && id != null){

            Setmeal setmeal = new Setmeal();

            setmeal.setStatus(status);

            setmeal.setId(id);

            setmealMapper.update(setmeal);

        }


    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
